/**
 * 核心技术支持
 * IT渠道网 小程序商城
 * 13873174181 微信同号
 */
import {Order} from '../order/order-model.js';
var order = new Order();

Page({
    data: {
        pageIndex: 1,
        loadingHidden: false,
        restUrl: order.getUrl(),
        isLoadedAll: false,
        orderArr: []
    },
    /*
     * 获取状态
     */
    onLoad: function() {
        //this._getOrders();
        this.setData({
            loadingHidden: true
        })
    },
    /*
     * 获取订单数据
     */
    _getOrders: function(callback) {
        var that = this,
            param = {};
            param['page'] = this.data.pageIndex;
            param['state_type'] = this.data.state;
            param['client'] = 'min';
        order.getOrders(param, (data)=>{
            that.setData({
                loadingHidden: true
            })
            // 订单下拉,追加到[]中,显示追加效果
            if (data.length > 0) {
                that.data.orderArr.push.apply(that.data.orderArr, data);
                that.setData({
                    orderArr: that.data.orderArr
                })
            } else {
                // 最后无数据,显示加载完毕
                that.setData({
                    isLoadedAll: true
                })
                // 加载完page重置1
                // that.data.pageIndex = 1;
            }
            callback && callback();
        })
    },
    /**
     * 监听用户下拉动作  重新刷新用户订单,回到&page=1
     */
    onPullDownRefresh: function() {
        var that = this;
        this.data.orderArr = [];  // 订单初始化
        that.data.pageIndex = 1;
        this._getOrders(() => {
            that.setData({
                isLoadedAll: false
            })
            wx.stopPullDownRefresh();
            order.execSetStorageSync(false);  // 更新标志位 上已加,再加过滤中间发送订单情况
        });
    },
    /**
     * 页面上拉触底事件的处理函数  上拉到底部触发 &page=2,3..
     */
    onReachBottom: function() {
        if (!this.data.isLoadedAll) {
            this.data.pageIndex++;
            this._getOrders();
        }
    },
    /*
     * 显示订单信息
     */
    showOrderDetailInfo: function(event) {
        var id = order.getDataSet(event,'id');
        wx.navigateTo({
            url:'../order/order?from=order&id='+id
        });
    },
    /*
     * 提示窗口
     */
    showTips: function(title,content) {
        wx.showModal({
            title: title,
            content: content,
            showCancel: false,
            success: function(res) {
            }
        })
    }
})